#include "ns.h"

extern union Nsipc nsipcbuf;

void
input(envid_t ns_envid)
{
    int r=0;
    int n=0;
	struct jif_pkt *pkt = (struct jif_pkt *)0x00100000;
    envid_t envid   = sys_getenvid();
	binaryname = "ns_input";

	// LAB 6: Your code here:
	// 	- read a packet from the device driver
	//	- send it to the network server
	// Hint: When you IPC a page to the network server, it will be
	// reading from it for a while, so don't immediately receive
	// another packet in to the same physical page.
    if((r=sys_page_alloc(0, (void *)pkt, PTE_P|PTE_W|PTE_U))<0)
        panic("sys_page_map: error: %x\n", r);

    while (1) {
        if((n=sys_ns_recv(pkt->jp_data, PGSIZE-sizeof(struct jif_pkt)))>0) {
            if (n != 11) {
            pkt->jp_len = n;
            ipc_send(ns_envid, NSREQ_INPUT, (void *)pkt, PTE_P|PTE_W|PTE_U);
            }
            sys_yield();
        } 
        sys_yield();
    }

    if ((r=sys_page_unmap(0, (void *)pkt))<0)
        panic("sys_page_unmap: error: %x\n", r);
}
